export function fileToDataURL(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = function (e) {
      if (reader.result && typeof reader.result == "string") {
        resolve(reader.result);
      }
    };
    reader.onerror = function (e) {
      reject(e);
    };
    reader.readAsDataURL(file);
  });
}

export const dataURLToImage = (dataURL) => {
  return new Promise((resolve) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.src = dataURL;
  });
};

export const convertQualityToBit = (quality) => {
  let bit = 0;
  if (quality > 100 || quality <= 0) {
    bit = 0;
  } else {
    if(quality == 10) {
      bit = quality * 256 * 0.01;
    } else if(quality == 20) {
      bit = !quality ? 0 : quality * 256 * 0.15;
    } else if(quality == 30) {
      bit = !quality ? 0 : quality * 256 * 0.30;
    } else if(quality == 40) {
      bit = !quality ? 0 : quality * 256 * 0.45;
    } else if(quality == 50) {
      bit = !quality ? 0 : quality * 256 * 0.5;
    } else if(quality == 60) {
      bit = !quality ? 0 : quality * 256 * 0.55;
    } else if(quality == 70) {
      bit = !quality ? 0 : quality * 256 * 0.6;
    } else if(quality == 80) {
      bit = !quality ? 0 : quality * 256 * 0.75;
    } else if(quality == 90) {
      bit = !quality ? 0 : quality * 256 * 0.9;
    } else if(quality == 100) {
      bit = !quality ? 0 : quality * 256 * 1;
    } 
  }
  return bit;
};